﻿using System.Collections.Generic;
using System.Data;
using System.IO;

using Excel;

namespace BMCShowView.Business
{
    public static class EventWorkOrder
    {
        public class WorkOrder
        {
            public string Id { get; set; }
            public string From { get; set; }
            public string Customer { get; set; }
            public string MajorClass { get; set; }
            public string MinorClass { get; set; }
            public string Priority { get; set; }
            public string Status { get; set; }
            public string Handler { get; set; }
            public string Description { get; set; }
        }

        public static List<WorkOrder> ReadFromStream(Stream stream)
        {
            var result = new List<WorkOrder>();

            using (var reader = ExcelReaderFactory.CreateOpenXmlReader(stream))
            {
                reader.IsFirstRowAsColumnNames = true;
                using (var dataset = reader.AsDataSet())
                {
                    using (var table = dataset.Tables["事件工单"])
                    {
                        foreach (DataRow row in table.Rows)
                        {
                            result.Add(new WorkOrder
                            {
                                Id = row[0].ToString(),
                                From = row[1].ToString(),
                                Customer = row[2].ToString(),
                                MajorClass = row[3].ToString(),
                                MinorClass = row[4].ToString(),
                                Priority = row[5].ToString(),
                                Status = row[6].ToString(),
                                Handler = row[7].ToString(),
                                Description = row[8].ToString()
                            });
                        }
                    }
                }
            }

            return result;
        }
    }
}
